<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :inline="true">
      <el-form-item label="套餐名称" prop="bomName">
        <el-input v-model="form.bomName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="套餐编码" prop="bomCode">
        <el-input v-model="form.bomCode" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="定价方式" prop="priceType">
        <el-select v-model="form.priceType" filterable>
          <el-option
            v-for="item in dict.type.ats_price_type"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-button type="primary" plain icon="el-icon-plus" v-hasPermi="['ims:saleBom:add']" @click="updateRow(0,{})">新增
    </el-button>
    <el-button type="success" plain icon="el-icon-download" v-hasPermi="['ims:saleBom:export']" @click="handleExport">导出
    </el-button>
    <el-table
      v-loading="loading"
      :data="list"
    >
      <el-table-column type="index" label="序号" align="center">
        <template slot-scope="scope">
          <span>{{ (form.pageNum - 1) * form.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="bomName" label="套餐名称" align="center">
        <template slot-scope="scope">
          <el-link type="primary" @click="updateRow(1,scope.row)">{{ scope.row.bomName }}</el-link>
        </template>
      </el-table-column>
      <el-table-column prop="bomCode" label="套餐编码" align="center" width="150"/>
      <el-table-column prop="productName" label="商品名称" align="center"/>
      <el-table-column prop="totalPrice" label="套餐价格(元)" align="center" width="100"/>
      <el-table-column prop="productSource" label="商品来源" align="center" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_good_source" :value="scope.row.productSource"/>
        </template>
      </el-table-column>
      <el-table-column prop="priceType" label="定价方式" align="center" width="150">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ats_price_type" :value="scope.row.priceType"/>
        </template>
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间" align="center" width="150"/>
      <el-table-column label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="updateRow(1,scope.row)"
          >详情
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:saleBom:update']"
            @click="updateRow(2,scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            v-hasPermi="['ims:saleBom:delete']"
            v-if="scope.row.isUsed!=0"
            @click="updateRow(3,scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="form.pageNum"
      :limit.sync="form.pageSize"
      @pagination="getList"
    />
    <updateDialog ref="updateDialog" @ok="handleQuery"></updateDialog>
  </div>
</template>

<script>
import updateDialog from "./updateDialog.vue";
import {deleteSaleBom, listSaleBom} from "../../../../api/saleMng";
import {formatDay} from "../../../../utils";

export default {
  components: {updateDialog},
  dicts: ['ats_price_type', 'basic_good_source'],
  data() {
    return {
      loading: false,
      list: [],
      form: {
        bomName: "",
        bomCode: "",
        priceType: "",
        pageNum: 1,
        pageSize: 10
      },
      total: 0
    }
  },
  created() {
    this.load()
  },
  methods: {
    load() {
      this.handleQuery()
    },
    handleQuery() {
      this.form.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('form')
      this.handleQuery()
    },
    getList() {
      this.loading = true
      listSaleBom(this.form).then(res => {
        this.list = res.rows
        this.total = res.total
      }).finally(e => {
        this.loading = false
      })
    },
    handleExport() {
      this.download('/ims/sale/bom/export', {
        ...this.form
      }, `套餐列表_${formatDay(new Date())}.xlsx`)
    },
    updateRow(flag, row) {
      let form = JSON.parse(JSON.stringify(row))
      if (flag != 3) {
        this.$refs.updateDialog.show(flag, form)
      } else {
        this.$modal.confirm('是否确定删除').then(res => {
          deleteSaleBom({id: form.id}).then(res => {
            this.handleQuery()
            this.$message.success('删除成功')
          })
        })
      }
    }
  }
}
</script>
